<?php
/**
 * Course Class Model
 *
 * Copyright 2012
 * @date 21-April-2012
 * @author
 * @version 0.0.1
 **/
class Customer extends AppModel {
/**
 * Name
 *
 * @var string
 */
    const INACTIVE = 0;
    const ACTIVE = 1;
    public $name = 'Customer';
    public $belongsTo = array(
        'Country'=>array(
            'className'=>'Country',
            'foreignKey'=>'country_id'
        ),
        'City'=>array(
            'className'=>'City',
            'foreignKey'=>'city_id'
        ),
        'CustomerType'=>array(
            'className'=>'CustomerType',
            'foreignKey'=>'customer_type_id'
        )
    );
    public $validate = array(
            'f_name' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter your first name'
                )
            ),  
        
            'l_name' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter your last name'
                )
            ),  
            'birthday' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter your birth day'
                )
            ),  
            'email' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please enter Email'
                ),
                'rule-2' => array ( 
                    'rule' => 'isUnique',
                    'message' => 'Please enter other email'
                ),
                'rule-3' => array ( 
                    'rule' => 'email',
                    'message' => 'Please enter a valid email'
                )
            ), 
            'password' => array(
                'rule-1' => array(
                    'rule' => array('checkForPassword'),  
                    'message' => 'Password must be greater 5 character.'
                )
            ),
            'confirmPassword' => array(
                'rule-1' => array(
                    'rule' => array('identicalFieldValues', 'password'),  
                    'message' => 'Please confirm your password again.'
                )
            )  ,
            'country_id' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please choose your country'
                )
            ),    
            'city_id' => array (
                'rule-1' => array ( 
                    'rule' => 'notEmpty',
                    'message' => 'Please choose your city'
                )
            )
        );
/**
 * @method: identicalFieldValues
 * @purpose: to check if 2 values are equal
 * @param $field, $compare_fields
 * @return boolean
 **/
    function identicalFieldValues( $field = array(), $compare_field = null ) {
        foreach( $field as $key => $value ){
            $v1 = $value;
            $v2 = $_POST['data'][$this->name][$compare_field];

            if($v1 != $v2) {
                return false;
            } else {
                continue;
            }
        }
        return true; 
    }
    
/**
 * @method: checkForPassword
 * @purpose: to validate Password
 * @param none
 * @return boolean
 **/
    function checkForPassword() {
    
        $password = $_POST['data'][$this->name]['password'];

        $length = strlen($password);
        if($length < 5) { //Check for Length
            return false;
        } else {
            return true;  
        }
    }
    public function get_list(){
        $return = array();
        $customers= $this->find('all');
        foreach ($customers as $customer) {
            $return[$customer['Customer']['id']]=$customer['Customer']['f_name']." ".$customer['Customer']['l_name'];
        }
        return $return;
    }
    public static function get_name($id){
        $country = ClassRegistry::init('Customer');
        $cus = $country->read(null,$id);
        return $cus['Customer']['f_name']." ".$cus['Customer']['l_name'];
    }
    
    public function find_customer($customer_number,$card_number) {
        return $this->find('first',array('conditions'=>array('customer_number'=>$customer_number,'card_number'=>$card_number)));
    }


    public function beforeSave($options = array()) {
      if(isset($this->data['Customer']['password']))
        $this->data['Customer']['password'] = AuthComponent::password($this->data['Customer']['password']);
        return true;
    }
}